﻿using commonImportPlugin.Common;
using commonImportPlugin.Configs.MatchFile;
using ICMS.PluginInterfaces;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace commonImportPlugin.Core.FilesCore.MatchFile
{
    /// <summary>
    /// 匹配数据文件解析基础类
    /// </summary>
    public class MatchFileBase:CommonUtil
    {
        /// <summary>
        /// 获取每一个匹配文件的数据
        /// </summary>
        /// <param name="allMatchDatas">收集数据</param>
        /// <param name="medicalFilePath">匹配文件</param>
        /// <param name="config">配置</param>
        /// <returns>true 成功，false失败</returns>
        public bool GetFileMatchDatas(List<ICData> allMatchDatas, string medicalFilePath, MatchFileConfig config)
        {
            string[] originMatchLines = null;//所有初始匹配数据         
            ICData matchData = null;
            int[] subRules = null;

            if (!File.Exists(medicalFilePath))
                throw new Exception("匹配文件不存在！");
            try
            {
                originMatchLines = File.ReadAllLines(medicalFilePath, Encoding.GetEncoding(config.encoding));
                for (int i = config.dataIndex - 1; i < originMatchLines.Length; i++)
                {
                    //获取相应数据
                    #region 
                    string[] matchDatas = originMatchLines[i].Split(config.dataSeparators, StringSplitOptions.None);
                    matchData = new ICData();       
         
                    //银行卡号
                    if (config.subBankCardId != null)
                    {
                        subRules = config.subBankCardId;
                        int column = subRules[0];
                        int subStart = subRules[1];
                        int subEnd = subRules[2];
                        matchData.BankCardID = SubString(matchDatas[column - 1].Trim(), subStart, subEnd);
                    }

                    //身份证号
                    if (config.subPersonId != null)
                    {
                        subRules = config.subPersonId;
                        int column = subRules[0];
                        int subStart = subRules[1];
                        int subEnd = subRules[2];
                        matchData.PersonID = SubString(matchDatas[column - 1].Trim(), subStart, subEnd);
                    }

                    //社保卡号
                    if (config.subCardId != null)
                    {
                        subRules = config.subCardId;
                        int column = subRules[0];
                        int subStart = subRules[1];
                        int subEnd = subRules[2];
                        matchData.SSCardID = SubString(matchDatas[column - 1].Trim(), subStart, subEnd);
                    }
                          
                    allMatchDatas.Add(matchData);
                    #endregion
                }
            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
            }
            return true;
        }
    }
}
